/*
 * @function: 用于存储本地生成的永久或临时文件，文件的生命周期需要应用自己维护
 */

package me.weijing.common.http;

import java.io.File;
import java.io.IOException;

import me.weijing.common.util.MD5Util;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

public class FileStore {
	private static String sdcarRootPath=null;
	private static String dataRootPath=null;
	private static boolean bInited=false;
	private static int maxSubDirs=16;
	
	public static boolean isSDCardAvailable() {  
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
    }  
	public static boolean  isSDCardFull(){
		if(isSDCardAvailable()==false)
			return true;
		StatFs fs = new StatFs(Environment.getExternalStorageDirectory()
				.getAbsolutePath());
		return !(fs.getAvailableBlocks() > 1);
		
	}
	public static boolean isInnerStorageFull(){
		StatFs fs = new StatFs(dataRootPath);
//		return !(fs.getAvailableBlocks() > 1);
		//must be larger than 64MB
		return !(fs.getAvailableBlocks()*fs.getBlockSize()>67108864);
	}
	
	public static void Init(Context context ){
		if(bInited)
			return;
		//dataCacheCapacity
		
		String pkgName=context.getPackageName();
		sdcarRootPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+pkgName+"/data/";
		dataRootPath = context.getFilesDir().getAbsolutePath()+"/"+pkgName+"/data/";
		
		
		File fp=new File(dataRootPath);
		if(!fp.exists()){
			fp.mkdirs();
		}
		File f = new File(dataRootPath + ".nomedia");
		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		for(int i=0;i<maxSubDirs;i++){
			String subdir=dataRootPath+String.valueOf(i);
			fp=new File(subdir);
			if(!fp.exists())
				fp.mkdirs();
		}
		
		
		if(isSDCardAvailable()){
			fp=new File(sdcarRootPath);
			if(!fp.exists()){
				fp.mkdirs();
			}
			f = new File(sdcarRootPath + ".nomedia");
			if (!f.exists()) {
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			for(int i=0;i<maxSubDirs;i++){
				String subdir=sdcarRootPath+String.valueOf(i);
				fp=new File(subdir);
				if(!fp.exists())
					fp.mkdirs();
			}
			
		}

		bInited=true;
	}
	public static String genNewFilePath(){
		return genNewFilePath("");
	}
	/*
	 * @param: postfix ext: .jpg .png .amr
	 */
	public static String genNewFilePath(String postfix){
		java.util.UUID a= java.util.UUID.randomUUID();
		String uuidstr=a.toString();
		String md5url=MD5Util.md5(uuidstr);
		md5url=String.valueOf(Math.abs(md5url.hashCode())%maxSubDirs)+File.separator+md5url;
		String realfile=null;
		
		if(!isSDCardFull()){
			realfile=sdcarRootPath+md5url;
		}else{
			if(!isInnerStorageFull()){
				realfile=dataRootPath+md5url;
			}else{
				realfile=sdcarRootPath+md5url;
			}
		}
		String returnPath=null;
		if(TextUtils.isEmpty(postfix)){
			returnPath= realfile;
		}
		else{
			returnPath= realfile+postfix;
		}
		try{
			File f = new File(returnPath);
			File pf=new  File(f.getParent());
			pf.mkdirs();
		}catch(Exception e){
			e.printStackTrace();
		}
		return returnPath;
	}
	
	public static String getSdcarRootPath() {
		return sdcarRootPath;
	}
}
